//
//  UIViewController+STPresent.m
//  SportHome
//
//  Created by stoneobs on 16/11/25.
//  Copyright © 2016年 stoneobs. All rights reserved.
//

#import "UIViewController+STPresent.h"
static UIImagePickerController *  imagePicker;
static IMAGEPICKER                imgePickerBlock;
@implementation UIViewController (STPresent)

#pragma mark --Alert和Actionsheet
- (void)showAlert:(NSString*)message
{
    UIAlertController * art =[UIAlertController alertControllerWithTitle:@"请注意" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * cancel =[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];
    [art addAction:cancel];
    [self presentViewController:art animated:YES completion:nil];
}
- (void)showAlert:(NSString *)message andWithBlock:(ALERTBLOCK)finsh
{
    UIAlertController * vc =[UIAlertController alertControllerWithTitle:@"请注意" message:message preferredStyle:UIAlertControllerStyleAlert];
    [vc addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (finsh) {
            finsh(@"确认");
        }
    }]];
    [self presentViewController:vc animated:YES completion:nil];
    
}
- (void)showAlertCancelAndConfirm:(NSString*)message andWithBlock:(ALERTBLOCK)finsh
{
    UIAlertController * vc =[UIAlertController alertControllerWithTitle:@"请注意" message:message preferredStyle:UIAlertControllerStyleAlert];
    [vc addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (finsh) {
            finsh(@"取消");
        }
    }]];
    [vc addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (finsh) {
            finsh(@"确认");
        }
    }]];
    
    
    [self presentViewController:vc animated:YES completion:nil];
    
    
}
- (void)showAlertCustomTitleOne:(NSString *)one Two:(NSString*)two  message:(NSString*)message andWithBlock:(ALERTBLOCK)finsh
{
    UIAlertController * vc =[UIAlertController alertControllerWithTitle:@"请注意" message:message preferredStyle:UIAlertControllerStyleAlert];
    [vc addAction:[UIAlertAction actionWithTitle:one style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (finsh) {
            finsh(one);
        }
    }]];
    [vc addAction:[UIAlertAction actionWithTitle:two style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (finsh) {
            finsh(two);
        }
    }]];
    
    
    [self presentViewController:vc animated:YES completion:nil];
    
}
- (void)showActionSheet:(NSArray<NSString *> *)strArray andWithBlock:(ACTIONBLOCK)test
{
    
    UIAlertController * sheet =[UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    for (int i = 0; i< strArray.count; i++) {
        UIAlertAction * action =[UIAlertAction actionWithTitle:strArray[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (test) {
                test(i);
            }
            
            
        }];
        [sheet addAction:action];
    }
    
    [self presentViewController:sheet animated:YES completion:^{
        
    }];
}
#pragma mark ----默认image picker 和他的delegate
-(void)showDefultImagePicker:(IMAGEPICKER)pickerBlock
{
    UIImagePickerController * vc =[[UIImagePickerController alloc]init];
    vc.allowsEditing = YES;
    vc.delegate = self;
    imagePicker = vc;
    UIAlertController * sheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * camer = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (!TARGET_OS_SIMULATOR) {
           imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:imagePicker animated:YES completion:nil];
        }
        
    }];
    [sheet addAction:camer];
    UIAlertAction * photo =[UIAlertAction actionWithTitle:@"从相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:imagePicker animated:YES completion:nil];
    }];
    [sheet addAction:photo];
    UIAlertAction * cancle =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [sheet addAction:cancle];
    
    [self presentViewController:sheet animated:YES completion:nil];
    if (pickerBlock) {
        imgePickerBlock = pickerBlock;
    }
    
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [self dismissViewControllerAnimated:YES completion:^{
        UIImage *   image=[info objectForKey:@"UIImagePickerControllerEditedImage"];
        if (imgePickerBlock) {
            imgePickerBlock(image);
        }
    }];
}




@end
